﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using AgileShare.Framework.Core;

namespace AgileShare.Framework.Library.Helpers
{
    /// <summary>
    /// UserProfile操作类
    /// </summary>
    public static class SPUserProfileHepler
    {
        /// <summary>
        /// 按帐号获取用户配置
        /// </summary>
        /// <param name="strAccountName"></param>
        /// <returns></returns>
        public static UserProfile GetUserProfile(string strAccountName)
        {
            UserProfile userinfo = null;
            try
            {
                SPServiceContext context = SPServiceContext.Current;
                UserProfileManager userProfileManager = new UserProfileManager(context);
                if (userProfileManager.UserExists(strAccountName))
                {
                    userinfo = userProfileManager.GetUserProfile(strAccountName);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
            }
            return userinfo;
        }

        /// <summary>
        /// 获取用户属性
        /// </summary>
        /// <param name="accountName"></param>
        /// <param name="propertyDisplayName">属性显示名称</param>
        /// <returns></returns>
        public static object GetUserPropertyByDisplayName(this UserProfile userProfile, string propertyDisplayName)
        {
            object userPropertyValue = null;
            try
            {
                if (userProfile != null)
                {
                    var profileSubtypeProperty =
                        userProfile.Properties.FirstOrDefault(propertyBase => propertyBase.DisplayName == propertyDisplayName);
                    if (profileSubtypeProperty != null)
                    {
                        userPropertyValue = userProfile.GetProfileValueCollection(profileSubtypeProperty.Name).Value;
                    }
                }
            }
            catch (Exception ex) {
                string errorMessage = string.Format("Not Fonud User Profile Protery:{0}", propertyDisplayName);
                TraceProvider.Log(errorMessage, TraceProviderCategories.Default,
                    (uint)TraceProviderEventIds.SPListHelper, TraceProviderSeverity.Information, ex);
            }
            return userPropertyValue;
        }

        /// <summary>
        /// 获取用户属性
        /// </summary>
        /// <param name="accountName"></param>
        /// <param name="propertyDisplayName">属性显示名称</param>
        /// <returns></returns>
        public static object GetUserPropertyByName(this UserProfile userProfile, string propertyName)
        {
            object userPropertyValue = null;
            try
            {
                if (userProfile != null)
                {
                    var profileSubtypeProperty =
                        userProfile.Properties.FirstOrDefault(propertyBase => propertyBase.Name == propertyName);
                    if (profileSubtypeProperty != null)
                    {
                        userPropertyValue = userProfile.GetProfileValueCollection(profileSubtypeProperty.Name).Value;
                    }
                }
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("Not Fonud User Profile Protery:{0}", propertyName);
                TraceProvider.Log(errorMessage, TraceProviderCategories.Default,
                    (uint)TraceProviderEventIds.SPListHelper, TraceProviderSeverity.Information, ex);
            }
            return userPropertyValue;
        }

        /// <summary>
        /// 获取用户属性
        /// </summary>
        /// <param name="accountName"></param>
        /// <param name="propertyDisplayName">属性显示名称</param>
        /// <returns></returns>
        public static object GetUserPropertyByDisplayName(string accountName, string propertyDisplayName)
        {
            object userPropertyValue = null;
            try
            {
                UserProfile userProfile = GetUserProfile(accountName);
                if (userProfile != null)
                {
                    var profileSubtypeProperty =
                        userProfile.Properties.First(propertyBase => propertyBase.DisplayName == propertyDisplayName);
                    if (profileSubtypeProperty != null)
                    {
                        userPropertyValue = userProfile.GetProfileValueCollection(profileSubtypeProperty.Name).Value;
                    }
                }
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("Not Fonud User Profile Protery:{0}", propertyDisplayName);
                TraceProvider.Log(errorMessage, TraceProviderCategories.Default,
                    (uint)TraceProviderEventIds.SPListHelper, TraceProviderSeverity.Information, ex);
            }            
            return userPropertyValue;
        }

        /// <summary>
        /// 获取用户属性
        /// </summary>
        /// <param name="accountName"></param>
        /// <param name="propertyDisplayName">属性显示名称</param>
        /// <returns></returns>
        public static object GetUserPropertyByName(string accountName, string propertyName)
        {
            object userPropertyValue = null;
            try
            {
                UserProfile userProfile = GetUserProfile(accountName);
                if (userProfile != null)
                {
                    var profileSubtypeProperty =
                        userProfile.Properties.First(propertyBase => propertyBase.Name == propertyName);
                    if (profileSubtypeProperty != null)
                    {
                        userPropertyValue = userProfile.GetProfileValueCollection(profileSubtypeProperty.Name).Value;
                    }
                }
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("Not Fonud User Profile Protery:{0}", propertyName);
                TraceProvider.Log(errorMessage, TraceProviderCategories.Default,
                    (uint)TraceProviderEventIds.SPListHelper, TraceProviderSeverity.Information, ex);
            }
            return userPropertyValue;
        }
    }
}
